
$(function() {
	Tip.emp('负责人');
	
	Tip.supplier();
	
	$('#supplier').blur(function() {
		var a = $(this).val();
		
		var body = { };
		body.id = a;
		$.getJSON('contact.php?do=Supplier.ajax_id', body, function(json) {
			$("#c_name").val(json.name);
			$("#c_fullname").val(json.fullname);
			$("#c_address").val(json.address);
			$("#c_web").val(json.url);
		});
		
		var body = {};
		body.q = a;
		
		$.getJSON('finance.php?do=PaymentMy.ajax_linkman', body, function(json) {			
			Supplier.linkman(json);
		});
	});
	
	$("#label_linkman").hide();
	
	$('#date').datepicker(_date1_option);
	
	$('#currency').change(function() {
		var a = $("#total").val();
		var b = $(this).find("option:selected").text();
	
		var r = Currency.upper(a);
		
		var s = $("#label_total");		
		s.text(b + " " + r);
	});
	
	$('textarea').css("width", 400);
	
	var grid;
	var columns = [{
	    id: "#",
	    name: "",
	    width: 40,
	    behavior: "selectAndMove",
	    unselectable: true,
	    resizable: false,
	    cssClass: "cell-reorder"
	}, {
	    id: "mold",
	    name: "产品",
	    field: "mold_x",
	    width: 150,
	    cssClass: "cell-title",
	    editor: MoldCellEditor
	}, {
	    id: "name",
	    name: "工件",
	    field: "name",
	    width: 150,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "type",
	    name: "类型",
	    field: "type",
	    width: 80,
	    cssClass: "cell-title",
	    editor: TypeSelectCellEditor,
	    validator: requiredFieldValidator
	}, {
	    id: "date",
	    name: "完成日期",
	    field: "date",
	    width: 100,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Date,
	    validator: requiredFieldValidator
	}, {
	    id: "amount",
	    name: "费用",
	    field: "amount",
	    width: 100,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "content",
	    name: "备注",
	    field: "content",
	    width: 100,
	    cssClass: "cell-title",
	    editor: Slick.Editors.Text,
	    validator: requiredFieldValidator
	}, {
	    id: "ok",
	    name: "",
	    field: "ok",
	    width: 80,
	    cssClass: "cell-effort-driven",	    
	    cannotTriggerInsert: true,
	    formatter: Slick.Formatters.YesNo,
	    editor: Slick.Editors.Checkbox
	}];

	var options = {
			editable: true,
		    enableAddRow: true,
		    enableCellNavigation: true,
		    asyncEditorLoading: false,
		    rowHeight: 30
		};	
	
	grid = new Slick.Grid($("#myGrid"), data, columns, options);
	
	grid.onAddNewRow.subscribe(function (e, args) {
	// grid.onAddNewRow = function addItem(newItem,columnDef) {
        var item = {date:"", use:"", amount:"0.00", ok: true};
        item.date = current_date();
        
        $.extend(item, args.item);
        data.push(item);
        grid.invalidateRows([data.length - 1]);
        grid.updateRowCount();
        grid.render();
        
        total();
    });
    
	grid.onCellChange.subscribe(function(e, args) {
    // grid.onCellChange = function(currentRow, currentCell, item) {
		item = args.item;
		currentCell = args.cell;
		currentRow = args.row;
		
    	total();
    });  
});

function total() {
	var total = 0;
	$.each(data, function(k, v) {
		if (v.ok) {
			total += parseFloat(v.amount);
		}		
	});
		
	$("#amount_t").html(total);
	$("#amount").val(total);
	
	var b = $("#currency").find("option:selected").text();
	Currency.upper(total, b, "#label_amount");
}
